<template>
  <div class="post-detail" :class="theme">
    <header class="post-header">
      <div class="header-content">
        <router-link to="/Forum" class="back-button">
          <svg viewBox="0 0 24 24" width="24" height="24">
            <path fill="currentColor" d="M20 11H7.83l5.59-5.59L12 4l-8 8 8 8 1.41-1.41L7.83 13H20v-2z" />
          </svg>
          Back
        </router-link>
        <button @click="toggleTheme" class="theme-toggle">
          <svg viewBox="0 0 24 24" width="20" height="20">
            <path fill="currentColor" v-if="theme === 'light'"
              d="M12,7c-2.76,0-5,2.24-5,5s2.24,5,5,5s5-2.24,5-5S14.76,7,12,7L12,7z" />
            <path fill="currentColor" v-else
              d="M12,3c-4.97,0-9,4.03-9,9s4.03,9,9,9s9-4.03,9-9c0-0.46-0.04-0.92-0.1-1.36c-0.98,1.37-2.58,2.26-4.4,2.26 c-2.98,0-5.4-2.42-5.4-5.4c0-1.81,0.89-3.42,2.26-4.4C12.92,3.04,12.46,3,12,3L12,3z" />
          </svg>
        </button>
      </div>
    </header>

    <main class="post-content">
      <article class="post-article">
        <div class="post-meta">
          <span class="post-date">{{ formatDate(post.createdAt) }}</span>
          <span class="post-word-tag" v-if="post.word">#{{ post.word }}</span>
        </div>

        <h1 class="post-title">{{ post.title }}</h1>

        <div class="post-author">
          <div class="author-avatar">
            <img :src="post.author.avatar" :alt="post.author.name">
          </div>
          <div class="author-info">
            <span class="author-name">{{ post.author.name }}</span>
            <span class="author-bio">{{ post.author.bio }}</span>
          </div>
        </div>

        <div class="post-body" v-html="post.content"></div>

        <div class="post-tags">
          <span v-for="tag in post.tags" :key="tag" class="tag">#{{ tag }}</span>
        </div>
      </article>

      <aside class="post-sidebar">
        <div class="sidebar-card">
          <h3>Related Posts</h3>
          <ul class="related-posts">
            <li v-for="related in relatedPosts" :key="related.id">
              <router-link :to="`/post/${related.id}`">{{ related.title }}</router-link>
            </li>
          </ul>
        </div>

        <div class="sidebar-card">
          <h3>About the Author</h3>
          <div class="author-card">
            <img :src="post.author.avatar" :alt="post.author.name" class="author-avatar"
              style="width: 50%; height: 50%;">
            <div>
              <h4>{{ post.author.name }}</h4>
              <p>{{ post.author.bio }}</p>
              <p class="author-stats">{{ post.author.followers }} followers · {{ post.author.posts }} posts</p>
            </div>
          </div>
        </div>
      </aside>
    </main>

    <footer class="post-footer">
      <div class="footer-content">
        <div class="post-actions">
          <button class="like-button" @click="toggleLike">
            <svg viewBox="0 0 24 24" width="20" height="20">
              <path fill="currentColor" :class="{ 'liked': post.isLiked }"
                d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z" />
            </svg>
            {{ post.likes }} Likes
          </button>
          <button class="share-button">
            <svg viewBox="0 0 24 24" width="20" height="20">
              <path fill="currentColor"
                d="M18 16.08c-.76 0-1.44.3-1.96.77L8.91 12.7c.05-.23.09-.46.09-.7s-.04-.47-.09-.7l7.05-4.11c.54.5 1.25.81 2.04.81 1.66 0 3-1.34 3-3s-1.34-3-3-3-3 1.34-3 3c0 .24.04.47.09.7L8.04 9.81C7.5 9.31 6.79 9 6 9c-1.66 0-3 1.34-3 3s1.34 3 3 3c.79 0 1.5-.31 2.04-.81l7.12 4.16c-.05.21-.08.43-.08.65 0 1.61 1.31 2.92 2.92 2.92 1.61 0 2.92-1.31 2.92-2.92s-1.31-2.92-2.92-2.92z" />
            </svg>
            Share
          </button>
        </div>

        <div class="post-comments">
          <h3>Comments ({{ post.comments.length }})</h3>
          <div class="comment-form">
            <textarea placeholder="Add a comment..." v-model="newComment"></textarea>
            <button @click="addComment" :disabled="!newComment.trim()">Post</button>
          </div>
          <ul class="comments-list">
            <li v-for="comment in post.comments" :key="comment.id" class="comment">
              <div class="comment-avatar">
                <img :src="comment.author.avatar" :alt="comment.author.name">
              </div>
              <div class="comment-content">
                <div class="comment-header">
                  <span class="comment-author">{{ comment.author.name }}</span>
                  <span class="comment-date">{{ formatDate(comment.createdAt) }}</span>
                </div>
                <p class="comment-text">{{ comment.text }}</p>
              </div>
            </li>
          </ul>
        </div>
      </div>
    </footer>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, inject } from 'vue'
import { useRoute } from 'vue-router'

const theme = inject("theme", ref('light'));
const toggleTheme = () => {
  theme.value = theme.value === 'light' ? 'dark' : 'light';
};

const route = useRoute()
const postId = route.params.id
const newComment = ref('')

// Mock data - in a real app, you'd fetch this from an API
const post = ref({
  id: postId,
  title: 'The Art of Writing Clean Code',
  content: '<p>Writing clean code is an essential skill for any developer. It not only makes your code more maintainable but also helps other developers understand your thought process.</p><p>Here are some key principles:</p><ul><li>Meaningful variable and function names</li><li>Small, focused functions</li><li>Consistent formatting</li><li>Avoid deep nesting</li><li>Proper error handling</li></ul><p>Remember, code is read more often than it is written.</p>',
  word: 'coding',
  tags: ['programming', 'best-practices', 'development'],
  createdAt: '2023-05-15T10:30:00Z',
  likes: 42,
  isLiked: false,
  author: {
    name: 'Jane Developer',
    avatar: 'https://randomuser.me/api/portraits/women/44.jpg',
    bio: 'Senior Software Engineer | Clean Code Advocate',
    followers: 1200,
    posts: 56
  },
  comments: [
    {
      id: 1,
      text: 'Great article! I especially agree with the point about small functions.',
      createdAt: '2023-05-15T11:45:00Z',
      author: {
        name: 'John Coder',
        avatar: 'https://randomuser.me/api/portraits/men/32.jpg'
      }
    },
    {
      id: 2,
      text: 'Could you elaborate more on error handling patterns?',
      createdAt: '2023-05-16T09:15:00Z',
      author: {
        name: 'Alex Programmer',
        avatar: 'https://randomuser.me/api/portraits/men/75.jpg'
      }
    }
  ]
})

const relatedPosts = ref([
  { id: '2', title: '10 JavaScript Best Practices You Should Know' },
  { id: '3', title: 'The Power of Functional Programming' },
  { id: '4', title: 'Refactoring Legacy Code: A Practical Guide' }
])

function formatDate(dateString) {
  const date = new Date(dateString)
  return date.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' })
}

function toggleLike() {
  post.value.isLiked = !post.value.isLiked
  post.value.likes += post.value.isLiked ? 1 : -1
}

function addComment() {
  if (!newComment.value.trim()) return

  const comment = {
    id: Date.now(),
    text: newComment.value,
    createdAt: new Date().toISOString(),
    author: {
      name: 'Current User',
      avatar: 'https://randomuser.me/api/portraits/lego/5.jpg'
    }
  }

  post.value.comments.unshift(comment)
  newComment.value = ''
}

// In a real app, you would fetch the post data here
onMounted(() => {
  // fetchPost()
})
</script>

<style scoped>
.post-detail {
  --bg-color: #f8f9fa;
  --text-color: #333;
  --primary-color: #4361ee;
  --secondary-color: #6c757d;
  --border-color: #e1e4e8;
  --card-bg: #fff;
  --hover-bg: #f1f3f5;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  min-height: 100vh;
  background-color: var(--bg-color);
  color: var(--text-color);
  transition: all 0.3s ease;
}

.post-detail.dark {
  --bg-color: #1a1a1a;
  --text-color: #e0e0e0;
  --primary-color: #4895ef;
  --secondary-color: #8e8e8e;
  --border-color: #333;
  --card-bg: #2d2d2d;
  --hover-bg: #383838;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.post-header {
  border-bottom: 1px solid var(--border-color);
  padding: 1rem 0;
  background-color: var(--card-bg);
  box-shadow: var(--shadow);
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 2rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.back-button {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--primary-color);
  text-decoration: none;
  font-weight: 500;
}

.back-button:hover {
  text-decoration: underline;
}

.theme-toggle {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-color);
  padding: 0.5rem;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.theme-toggle:hover {
  background-color: var(--hover-bg);
}

.post-content {
  max-width: 1200px;
  margin: 2rem auto;
  padding: 0 2rem;
  display: grid;
  grid-template-columns: 1fr 300px;
  gap: 2rem;
}

.post-article {
  background-color: var(--card-bg);
  border-radius: 8px;
  padding: 2rem;
  box-shadow: var(--shadow);
}

.post-meta {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1rem;
  color: var(--secondary-color);
  font-size: 0.9rem;
}

.post-word-tag {
  color: var(--primary-color);
  font-weight: 500;
}

.post-title {
  font-size: 2rem;
  margin: 1rem 0;
  line-height: 1.3;
  color: var(--text-color);
}

.post-author {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin: 2rem 0;
  padding: 1rem 0;
  border-top: 1px solid var(--border-color);
  border-bottom: 1px solid var(--border-color);
}

.author-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  overflow: hidden;
}

.author-avatar>img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.author-info {
  display: flex;
  flex-direction: column;
}

.author-name {
  font-weight: 600;
}

.author-bio {
  font-size: 0.9rem;
  color: var(--secondary-color);
}

.post-body {
  line-height: 1.6;
  margin-bottom: 2rem;
}

.post-body :deep(p) {
  margin-bottom: 1.5rem;
}

.post-body :deep(ul) {
  margin-bottom: 1.5rem;
  padding-left: 1.5rem;
}

.post-body :deep(li) {
  margin-bottom: 0.5rem;
}

.post-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 2rem;
}

.tag {
  background-color: var(--hover-bg);
  padding: 0.3rem 0.8rem;
  border-radius: 20px;
  font-size: 0.8rem;
  color: var(--secondary-color);
}

.post-sidebar {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.sidebar-card {
  background-color: var(--card-bg);
  border-radius: 8px;
  padding: 1.5rem;
  box-shadow: var(--shadow);
}

.sidebar-card h3 {
  margin-top: 0;
  margin-bottom: 1rem;
  font-size: 1.2rem;
}

.related-posts {
  list-style: none;
  padding: 0;
  margin: 0;
}

.related-posts li {
  margin-bottom: 0.8rem;
}

.related-posts a {
  color: var(--primary-color);
  text-decoration: none;
}

.related-posts a:hover {
  text-decoration: underline;
}

.author-card {
  display: flex;
  gap: 1rem;
  align-items: center;
}

.author-card .author-avatar {
  width: 60px;
  height: 60px;
}

.author-card h4 {
  margin: 0 0 0.3rem 0;
}

.author-card p {
  margin: 0.3rem 0;
  font-size: 0.9rem;
}

.author-stats {
  color: var(--secondary-color);
  font-size: 0.8rem !important;
}

.post-footer {
  max-width: 1200px;
  margin: 0 auto 3rem auto;
  padding: 0 2rem;
}

.footer-content {
  background-color: var(--card-bg);
  border-radius: 8px;
  padding: 2rem;
  box-shadow: var(--shadow);
}

.post-actions {
  display: flex;
  gap: 1rem;
  margin-bottom: 2rem;
}

.like-button,
.share-button {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  border-radius: 20px;
  border: 1px solid var(--border-color);
  background-color: var(--card-bg);
  color: var(--text-color);
  cursor: pointer;
  transition: all 0.2s ease;
}

.like-button:hover,
.share-button:hover {
  background-color: var(--hover-bg);
}

.like-button svg path {
  fill: var(--text-color);
}

.like-button.liked svg path,
.liked {
  fill: #ff4757;
}

.post-comments h3 {
  margin-top: 0;
  margin-bottom: 1.5rem;
}

.comment-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-bottom: 2rem;
}

.comment-form textarea {
  width: 96%;
  min-height: 100px;
  padding: 2%;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  background-color: var(--card-bg);
  color: var(--text-color);
  resize: vertical;
}

.comment-form button {
  align-self: flex-end;
  padding: 0.5rem 1.5rem;
  background-color: var(--primary-color);
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.comment-form button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.comments-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.comment {
  display: flex;
  gap: 1rem;
  padding: 1rem 0;
  border-bottom: 1px solid var(--border-color);
}

.comment:last-child {
  border-bottom: none;
}

.comment-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  flex-shrink: 0;
}

.comment-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.comment-content {
  flex-grow: 1;
}

.comment-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}

.comment-author {
  font-weight: 600;
}

.comment-date {
  color: var(--secondary-color);
  font-size: 0.8rem;
}

.comment-text {
  margin: 0;
  line-height: 1.5;
}

@media (max-width: 768px) {
  .post-content {
    grid-template-columns: 1fr;
    padding: 0 1rem;
  }

  .post-sidebar {
    order: -1;
  }

  .post-title {
    font-size: 1.5rem;
  }
}
</style>